package com.yanxu.proxy.db;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025/4/15-2025
 * @Description: 这里用一句话描述这个类的作用
 */
@Configuration
public class ConfigData {

    /**
     *     spring:
     *       application:
     *         name: java-propagation
     *       datasource:
     *         master:
     *           type: com.zaxxer.hikari.HikariDataSource
     *           driver-class-name: com.mysql.cj.jdbc.Driver
     *           url: jdbc:mysql://192.168.1.30:3306/framework_study?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&rewriteBatchedStatements=true
     *           username: root
     *           password: Root@123456
     *         slave:
     *           type: com.zaxxer.hikari.HikariDataSource
     *           driver-class-name: com.mysql.cj.jdbc.Driver
     *           url: jdbc:mysql://192.168.1.30:3307/microservices?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
     *           username: root
     */

    //第一个数据源
    @Bean
    @ConfigurationProperties(prefix = "datasource.master")
    public DataSource dataSource1() {
        return DataSourceBuilder.create().build();
    }

    //第二个数据源
    @Bean
    @ConfigurationProperties(prefix = "datasource.slave")
    public DataSource dataSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DynamicDataSource dynamicDataSource() {
        //设置数据源
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("dataSource1",dataSource1());
        targetDataSources.put("dataSource2",dataSource2());

        //配置动态数据源
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // -- 所有的数据源 TargetDataSources
        dynamicDataSource.setTargetDataSources(targetDataSources);
        // -- 默认的数据源 DefaultTargetDataSource
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        return dynamicDataSource;
    }

    @Autowired
    private ApplicationContext applicationContext;
    @Bean
    @ConditionalOnMissingBean
    public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        //...
        Properties configLocationProperties = new Properties();
        configLocationProperties.put("mapperLocations","classpath:mapper/*.xml");
        factory.setConfigLocation(applicationContext.getResource(configLocationProperties.getProperty("mapperLocations")));
        //factory.setMapperLocations(null);
        return factory.getObject();
    }
}